import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/14 12:44
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1};
        solution.longestOnes(nums, 3);
    }
    public int longestOnes(int[] nums, int k) {
        int n = nums.length;
        int left = 0, right = 0, ans = 0;
        int count = 0;
        while (right < n) {
            if (nums[right] == 0) {
                count++;
            }
            while (count > k) {
                if (nums[left] == 0) {
                    count--;
                }
                left++;
            }
            ans = Math.max(ans, right - left + 1);
            right++;
        }
        return ans;
    }
}
